import requests

'''
1. 拿到contID
2. 拿到videoStatus 返回的json -> srcURL
3. 将srcURL 里面的内容进行修整
4. 下载视频
'''
# 发送请求
# 源地址
url = "https://www.pearvideo.com/video_1731014"
cont_id = url.split("_")[1]
# 视频访问链接
video_url = f"https://www.pearvideo.com/videoStatus.jsp?contId={cont_id}&mrd=0.20604976770175387"
# 请求头
head = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36",
    # 防盗链: 溯源, 当前本次请求的上一级是谁
    "referer": url
}

# 获取网页内容
video_resp = requests.get(video_url, headers=head)
# 以字典的方式返回数据
video_dic = video_resp.json()
# 视频数据
srcUrl = video_dic['videoInfo']['videos']['srcUrl']
# 系统时间数据
systemTime = video_dic['systemTime']
# 可下载的视频链接
srcUrl = srcUrl.replace(systemTime, f"cont-{cont_id}")

# 下载视频
with open("pear_video.mp4", mode='wb') as f:
    f.write(requests.get(srcUrl).content)

print('over')
video_resp.close()